﻿using LiveCharts;
using LiveCharts.Defaults;
using LiveCharts.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AgileLogistics.View.Baoxiu
{
    /// <summary>
    /// Home.xaml 的交互逻辑
    /// </summary>
    public partial class Home : Page
    {
        ViewModel.RepairViewModel vm;
        Model.DBContext dBContext = new Model.DBContext();
        public Home()
        {
            InitializeComponent();
            vm =this.DataContext as ViewModel.RepairViewModel;

            //获取数据
            vm.SumCount = dBContext.DB.Queryable<Model.Repairs>().Count();
            vm.HandleCount = dBContext.DB.Queryable<Model.Repairs>().Count(r =>r.State =="已完成"); //总票数
            vm.NewCount = dBContext.DB.Queryable<Model.Repairs>().Count(r => r.State =="待处理");//投票活动次数

            //集合数据
            //List<int> listInt=db.Queryable<Student>().Select(it => it.Id).ToList();
            vm.NewList = new List<int>();
            vm.HandleList = new List<int>();
            vm.XiaolvList = new List<int>();


            //近7天新增申请
            for (int i = 0; i <= 7; i++)
            {
                vm.NewList.Add(dBContext.DB.Queryable<Model.Repairs>().Where(m=>m.CreateTime >= DateTime.Now.Date.AddDays(i-7)  &&  m.CreateTime <DateTime.Now.Date.AddDays((i-7)+1)).Count());
            }

            //近7天通过申请
            for (int i = 0; i <= 7; i++)
            {
                vm.HandleList.Add(dBContext.DB.Queryable<Model.Repairs>().Where(m=>m.State=="已完成").Where(m => m.CreateTime >= DateTime.Now.Date.AddDays(i - 7) && m.CreateTime < DateTime.Now.Date.AddDays((i - 7) + 1)).Count());
            }

            //处理效率
            for (int i = 0; i <= 7; i++)
            {
                vm.XiaolvList.Add(dBContext.DB.Queryable<Model.Repairs>().Where(m => m.State == "已完成").Count());
            }

            

            for (int i = 0; i <= 7; i++)
            {
                vm.WeekLabels.Add(DateTime.Now.Date.AddDays(i - 7).Date.ToString("MM-dd"));
            }

            cc_Zhucai.Series.Add(new ColumnSeries
            {
                Values = new ChartValues<int>(vm.NewList), 
                DataLabels = true,
                LabelPoint = point => point.Y + ""
            }) ; 

            cc_Zhucai.AxisX.Add(new Axis
            {
                Labels = vm.WeekLabels,
              
                Separator = new LiveCharts.Wpf.Separator // force the separator step to 1, so it always display all labels
                {
                    Step = 1,
                    IsEnabled = false //disable it to make it invisible.
                },
                LabelsRotation = 15
            });

            cc_Zhucai.AxisY.Add(new Axis
            {
                LabelFormatter = value => value + "",
                Separator = new LiveCharts.Wpf.Separator()
            });


            cc_Chuli.Series.Add(new ColumnSeries
            {
                Values = new ChartValues<int>(vm.HandleList),
                DataLabels = true,
                LabelPoint = point => point.Y + ""
            });

            cc_Chuli.AxisX.Add(new Axis
            {
                Labels = vm.WeekLabels,

                Separator = new LiveCharts.Wpf.Separator // force the separator step to 1, so it always display all labels
                {
                    Step = 1,
                    IsEnabled = false //disable it to make it invisible.
                },
                LabelsRotation = 15
            });

            cc_Chuli.AxisY.Add(new Axis
            {
                LabelFormatter = value => value + "",
                Separator = new LiveCharts.Wpf.Separator()
            });
        }
    }
}
